//
//  MHMyClientListController.m
//  M-Saler
//
//  Created by Teresa on 2020/5/21.
//  Copyright © 2020 Teresa. All rights reserved.
//

#import "MHMyClientListController.h"
#import "MHMyClientListCell.h"
#import "MHMyClientListRequest.h"
#import "MHMyClientModel.h"
#import "MHMyClientDetailController.h"

@interface MHMyClientListController ()
@property (copy, nonatomic) NSArray<MHMyClientModel*> *dataList;
@end

@implementation MHMyClientListController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的客户";
    [MHMyClientListCell registerToTableView:self.tableView];
    [self configNavBarWhite];
    self.tableView.tableFooterView = [UIView new];

}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self fetchData];
}

-(void)fetchData{
    MHMyClientListRequest *req = [MHMyClientListRequest new];
    req.salesman_id = [MHUserModel userModel].theID;
    BLOCKSELF;
    [req asyncRequestWithsuccessHandler:^(BaseResponse *response) {
        blockSelf.dataList = response.data;
        [blockSelf.tableView reloadData];
    } failHandler:^(BaseResponse *response) {
        [[HUDHelper sharedInstance]tipMessage:response.message];
    }];
}



#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataList.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [MHMyClientListCell height];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MHMyClientListCell *cell = [tableView dequeueReusableCellWithIdentifier:MHMyClientListCell.nameOfClass forIndexPath:indexPath];
    cell.model = self.dataList[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    MHMyClientDetailController *newVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil]instantiateViewControllerWithIdentifier:@"MHMyClientDetailController"];
    newVC.model = self.dataList[indexPath.row];
    [self.navigationController pushViewController:newVC animated:YES];
}

@end
